﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InnovTech.Data.Behavior
{

    public interface IValidationResult 
    {
        bool IsSuccessful { get; }
    }
    public class ValidationException : Exception 
    {
        protected IValidationResult _result;
        public ValidationException(IValidationResult result)
        {
            _result = result;
        }
    }

    public abstract class ValidatorBase<TTarget, TParameter, TResult> : BehaviorBase<TTarget, TParameter> where TResult : IValidationResult
    {

        private TResult _result;


        public ValidatorBase(TTarget target)
            : base(target)
        {
        }

        public TResult ValidationResult 
        { 
            get { return _result; }
            protected set { _result = value; }
        }

        public override void Execute(TParameter parameter)
        {
            Validate(parameter);
            if (_result == null)
                throw new NotSupportedException("This Validator is not for the given target object");
            if (!_result.IsSuccessful)
                throw new ValidationException(_result);
        }

        protected abstract TResult ValidateCore(TParameter parameter);

        public TResult Validate(TParameter parameter)
        {
            _result = ValidateCore(parameter);
            return _result;
        }
    }
}
